GBML manual / Lists

list_add

Adds value to the end of target. Returns true if it fit, false if target was already full (nothing is added - target never crashes or corrupts, it just stops growing).

Syntax

list_add(target, value) -> bool

Arguments

NameType
targetlist
valueelement

Returns

bool

Description

Adds value to the end of target, a list<T> - the container type for things that come and go while a game runs, an inventory or a high-score table being the two most common. Returns true if it fit, false if target was already full.

target never crashes or corrupts when it is full - list_add checks first, so a beginner cannot break their own game by collecting one coin too many. If the return value is ignored, target simply stops growing until something is removed; check it only when the game needs to react to "no room left" (showing a message, say).

When the list is full, it is the new item that is not added; everything already in the list stays. list_insert is the other way round: on a full list it keeps the new item and the last item is lost. See Lists for both side by side.

array<T> is refused here - it has a fixed number of slots on purpose (see Variables, types and scope) and cannot grow. Declare the variable list<T> instead if it needs to.

Example

/// @type {list<int>}
var scores = [];
list_add(scores, 10);
var added = list_add(scores, 20); // true - there was room
A real capture - the list right after list_add(scores, 10), holding one itemA real capture - the list right after list_add(scores, 10), holding one item
A real capture - the list right after list_add(scores, 10), holding one item
The same list after list_add(scores, 20) too - both items now show, in orderThe same list after list_add(scores, 20) too - both items now show, in order
The same list after list_add(scores, 20) too - both items now show, in order

On the GBA

target is a real fixed-size vector of T items, up to N - N is target's own declared capacity (`/// @capacity`, default 8 if untagged). This checks target.full() before ever calling push_back, so an author who never checks the return value still cannot crash their own game by adding one item too many - the list simply stops accepting new items until something is removed. O(1).

See also: list_filllist_insertlist_removelist_lengthlist_clearlist_containsData containers